<?php

namespace App\Http\Controllers\Api;

use App\Models\Prize;
use App\Models\Product;
use App\Models\Company;
use Illuminate\Http\Request;
use App\Http\Requests\Api\PrizeRequest;
use App\Transformers\PrizeTransformer;

class PrizesController extends Controller
{
    public function search(Request $request, Prize $prize)
    {
        $per_page = $request->input('per_page', 20);
        $query = $prize->query();

        $query->where('delflag', false);

        if ($product_id = $request->product_id) {
            $query=$query->where('product_id', $request->product_id);
        }

        if ($status = $request->status) {
            $query=$query->where('status', $request->status);
        }

        if ($gtredeeming_quantity = $request->gtredeeming_quantity) {
            $query=$query->where('redeeming_quantity', '>=', $request->gtredeeming_quantity);
        }
        if ($ltredeeming_quantity = $request->ltredeeming_quantity) {
            $query=$query->where('redeeming_quantity', '<=', $request->ltredeeming_quantity);
        }

        if ($gtremain_quantity = $request->gtremain_quantity) {
            $query=$query->where('remain_quantity', '>=', $request->gtremain_quantity);
        }
        if ($ltremain_quantity = $request->ltremain_quantity) {
            $query=$query->where('remain_quantity', '<=', $request->ltremain_quantity);
        }

        if ($gtredeemed_quantity = $request->gtredeemed_quantity) {
            $query=$query->where('redeemed_quantity', '>=', $request->gtredeemed_quantity);
        }
        if ($ltredeemed_quantity = $request->ltredeemed_quantity) {
            $query=$query->where('redeemed_quantity', '<=', $request->ltredeemed_quantity);
        }

        if ($accumulate_point = $request->accumulate_point) {
            $query=$query->where('accumulate_point', $request->accumulate_point);
        }

        if (($shelves_begin = $request->shelves_begin) && ($shelves_end = $request->shelves_end)) {
            $query=$query->where('shelves_begin', '>=', $request->shelves_begin);
            $query=$query->where('shelves_end', '<=', $request->shelves_end);
        }

        if (($redeeming_begin = $request->redeeming_begin) && ($redeeming_end = $request->redeeming_end)) {
            $query=$query->where('redeeming_begin', '>=', $request->redeeming_begin);
            $query=$query->where('redeeming_end', '<=', $request->redeeming_end);
        }

        if (($redeemed_begin = $request->redeemed_begin) && ($redeemed_end = $request->redeemed_end)) {
            $query=$query->where('redeemed_begin', '>=', $request->redeemed_begin);
            $query=$query->where('redeemed_end', '<=', $request->redeemed_end);
        }

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $prizes = $query->paginate($per_page);

        return $this->response->paginator($prizes, new PrizeTransformer());
    }

	public function index(Request $request, Prize $prize)
    {
        $per_page = $request->input('per_page', 20);
        $query = $prize->query();

        $query->where('delflag', false);

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $prizes = $query->paginate($per_page);

        return $this->response->paginator($prizes, new PrizeTransformer());
    }

    public function companyIndex(Request $request, Company $company)
    {
        $per_page = $request->input('per_page', 20);

        $prizes=null;

        if ($orderBy = $request->orderBy) {
            $prizes = $company->prizes()->where('prizes.delflag', false)->ordered()
                ->paginate($per_page);
        } else {
            $prizes = $company->prizes()->where('prizes.delflag', false)->recent()
                ->paginate($per_page);
        }

        return $this->response->paginator($prizes, new PrizeTransformer());
    }

    public function show(Product $product, Prize $prize)
    {
        return $this->response->item($prize, new PrizeTransformer())
            ->setStatusCode(201);
    }

	public function store(PrizeRequest $request, Product $product)
	{
        $this->authorize('update', $prize);
		$prize = Prize::create($request->all());
		return $this->response->item($prize, new PrizeTransformer())
            ->setStatusCode(201);
	}

	public function update(PrizeRequest $request, Product $product, Prize $prize)
	{
        $this->authorize('update', $prize);
		$prize->update($request->all());

		return $this->response->item($prize, new PrizeTransformer())
            ->setStatusCode(201);
	}

	public function destroy(Product $product, Prize $prize)
	{
        $this->authorize('destroy', $prize);
		$prize->delflag=true;
        $prize->save();

		return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'删除成功',
        ])->setStatusCode(200);
	}

    public function attachProduct(Request $request, Prize $prize)
    {
        $this->authorize('update', $prize);
        $prize->product_id=$request->product_id;
        $prize->save();

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'关联成功',
        ])->setStatusCode(200);
    }
}